home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 64-bit_x64 / Japanese / cpsimage.cab / data / xipProcs / morphology.proc < prev    next >
Text File  |  2009-04-23  |  9KB  |  318 lines

  1. /*
  2. ** This script is a collection of software constructs for binary and grayscale
  3. ** morphological image processing.
  4. */
  5.  
  6. // Structuring element values for binary morphology
  7. #define SE_DONT_CARE  "-1";
  8. #define SE_MISS       "0";
  9. #define SE_HIT        "1";
  10.  
  11. // Class definition for rectangular structuring element
  12. CLASS STREL
  13. {
  14.   INTEGER width;
  15.   INTEGER height;
  16.   ELFLIST center;
  17.   ELFLIST data;
  18.  
  19.   METHOD toString ()
  20.     RETURNS (STRING sestr)
  21.   {
  22.     INTEGER i;
  23.     INTEGER count;
  24.  
  25.     sestr = this.width + " " + this.height;
  26.  
  27.     count = this.data.length ();
  28.     for (i=0; i<count; i++)
  29.        sestr = sestr +  " " + this.data[i];
  30.   }
  31. }
  32.  
  33. /* @XIPMakePepperStrel
  34.   // DESCRIPTION
  35.   Creates a rectangular structuring element for filtering pepper noise (small,
  36.   isolated black spots) from an image. The structuring element is returned as
  37.   the STREL object "strel".
  38.   
  39.   // ARGUMENTS
  40.   INTEGER level       This is the pepper removal level. It represents the
  41.                       maximum width and height of the black spots to be
  42.                       removed. The pepper removal level can be:
  43.                         1 to remove 1x1 spots
  44.                         2 to remove spots that can fit inside a 2x2 square
  45.                         3 to remove spots that can fit inside a 3x3 square
  46.                         4 to remove spots that can fit inside a 4x4 square
  47.   
  48.   // RETURNS
  49.   STREL   strel       Structuring element
  50. */
  51. private
  52. PROCEDURE XIPMakePepperStrel (INTEGER level)
  53.   RETURNS (STREL strel)
  54. {
  55.   INTEGER s;
  56.   INTEGER c;
  57.   INTEGER x;
  58.   INTEGER y;
  59.   INTEGER innerValue;
  60.  
  61.   if (level < 1) level = 1;
  62.   if (level > 4) level = 4;
  63.  
  64.   if (level == 1)
  65.     innerValue = SE_HIT;
  66.   else
  67.     innerValue = SE_DONT_CARE;
  68.  
  69.   s = level + 2;
  70.   c = (s - 1) / 2;
  71.  
  72.   strel.width  = s;
  73.   strel.height = s;
  74.   strel.center = (c, c);
  75.  
  76.   for (y=0; y<s; y++) {
  77.     for (x=0; x<s; x++) {
  78.       if (y < 1) {
  79.         strel.data.insert (obj: SE_MISS);
  80.       } else if (y < (level+1)) {
  81.         if (x < 1) {
  82.           strel.data.insert (obj: SE_MISS);
  83.         } else if (x < (level+1)) {
  84.           strel.data.insert (obj: innerValue);
  85.         } else {
  86.           strel.data.insert (obj: SE_MISS);
  87.         }
  88.       } else {
  89.         strel.data.insert (obj: SE_MISS);
  90.       }
  91.     }
  92.   }
  93. }
  94.  
  95. /* @XIPMakeSolidStrel
  96.   // DESCRIPTION
  97.   Creates a rectangular structuring element containing all hits, i.e. the
  98.   value of each member is set to SE_HIT. The structuring element is returned
  99.   as the STREL object "strel".
  100.   
  101.   // ARGUMENTS
  102.   INTEGER width       Width of structuring element
  103.   INTEGER height      Height of structuring element
  104.   
  105.   // RETURNS
  106.   STREL   strel       Structuring element
  107. */
  108. private
  109. PROCEDURE XIPMakeSolidStrel (INTEGER width, INTEGER height)
  110.   RETURNS (STREL strel)
  111. {
  112.   INTEGER size;
  113.   INTEGER cx;
  114.   INTEGER cy;
  115.   INTEGER idx;
  116.  
  117.   strel.width  = width;
  118.   strel.height = height;
  119.  
  120.   cx = (width - 1) / 2;
  121.   cy = (height - 1) / 2;
  122.   strel.center = (cx, cy);
  123.  
  124.   size = width * height;
  125.   for (idx=0; idx<size; idx++)
  126.     strel.data.insert (obj: SE_HIT);
  127. }
  128.  
  129. /* @XIPErode
  130.   // DESCRIPTION
  131.   Performs binary/gray morphological erosion on a given XIPIMAGE. The eroded
  132.   image is returned as the XIPIMAGE object "dst".
  133.   
  134.   // ARGUMENTS
  135.   XIPIMAGE src        Image to erode
  136.   STREL    strel      Structuring element
  137.   
  138.   // OPTIONAL ARGUMENTS
  139.   STRING   type       By specifying type as "binary", processing will route
  140.                       through XEngine erode_ipc module (adaptation of IPCore
  141.                       binary erosion routine that supports 1 bit grayscale
  142.                       images only). Otherwise, it routes through XEngine
  143.                       grayerode module (supports 8 bit grayscale images).
  144.   
  145.   // RETURNS
  146.   XIPIMAGE dst        Eroded image
  147. */
  148. private
  149. PROCEDURE XIPErode (XIPIMAGE src, STREL strel, STRING type)
  150.   RETURNS (XIPIMAGE dst)
  151. {
  152.   STRING sestr;
  153.  
  154.   // Need at least gray colorspace to process
  155.   if (src.getMember (member: "seps") > 1)
  156.     src = src.cspace (outspace: "gray");
  157.  
  158.   // Convert structuring element to a string
  159.   sestr = strel.toString ();
  160.  
  161.   // Do binary erosion if type is "binary"; otherwise do gray erosion
  162.   if (type == "binary") {
  163.     if (src.getMember (member: "bits") > 1)
  164.       src = src.convert (to: (1));
  165.  
  166.     dst = src.erode_ipc (sestring: sestr);
  167.   } else {
  168.     dst = src.invert ().grayerode (file: sestr).invert ();
  169.   }
  170. }
  171.  
  172. /* @XIPDilate
  173.   // DESCRIPTION
  174.   Performs binary/gray morphological dilation on a given XIPIMAGE. The dilated
  175.   image is returned as the XIPIMAGE object "dst".
  176.   
  177.   // ARGUMENTS
  178.   XIPIMAGE src        Image to dilate
  179.   STREL    strel      Structuring element
  180.   
  181.   // OPTIONAL ARGUMENTS
  182.   STRING   type       By specifying type as "binary", processing will route
  183.                       through XEngine dilate_ipc module (adaptation of IPCore
  184.                       binary dilation routine that supports 1 bit grayscale
  185.                       images only). Otherwise, it routes through XEngine
  186.                       graydilate module (supports 8 bit grayscale images).
  187.   
  188.   // RETURNS
  189.   XIPIMAGE dst        Dilated image
  190. */
  191. private
  192. PROCEDURE XIPDilate (XIPIMAGE src, STREL strel, STRING type)
  193.   RETURNS (XIPIMAGE dst)
  194. {
  195.   STRING sestr;
  196.  
  197.   // Need at least gray colorspace to process
  198.   if (src.getMember (member: "seps") > 1)
  199.     src = src.cspace (outspace: "gray");
  200.  
  201.   // Convert structuring element to a string
  202.   sestr = strel.toString ();
  203.  
  204.   // Do binary dilation if type is "binary"; otherwise do gray dilation
  205.   if (type == "binary") {
  206.     if (src.getMember (member: "bits") > 1)
  207.       src = src.convert (to: (1));
  208.  
  209.     dst = src.dilate_ipc (sestring: sestr);
  210.   } else {
  211.     dst = src.invert ().graydilate (file: sestr).invert ();
  212.   }
  213. }
  214.  
  215. /* @XIPOpen
  216.   // DESCRIPTION
  217.   Performs binary/gray morphological erosion followed by dilation on a given
  218.   XIPIMAGE using the same structuring element. The opened image is returned as
  219.   the XIPIMAGE object "dst".
  220.   
  221.   // ARGUMENTS
  222.   XIPIMAGE src        Image to open
  223.   STREL    strel      Structuring element
  224.   
  225.   // OPTIONAL ARGUMENTS
  226.   STRING   type       By specifying type as "binary", processing will route
  227.                       through XEngine erode_ipc and dilate_ipc modules
  228.                       (adaptations of IPCore binary erosion and dilation
  229.                       routines, respectively, that supports 1 bit grayscale
  230.                       images only). Otherwise, it routes through XEngine
  231.                       grayerode and graydilate modules (supports 8 bit
  232.                       grayscale images).
  233.   
  234.   // RETURNS
  235.   XIPIMAGE dst        Opened image
  236. */
  237. private
  238. PROCEDURE XIPOpen (XIPIMAGE src, STREL strel, STRING type)
  239.   RETURNS (XIPIMAGE dst)
  240. {
  241.   STRING sestr;
  242.  
  243.   // Need at least gray colorspace to process
  244.   if (src.getMember (member: "seps") > 1)
  245.     src = src.cspace (outspace: "gray");
  246.  
  247.   // Convert structuring element to a string
  248.   sestr = strel.toString ();
  249.  
  250.   // Do binary opening if type is "binary"; otherwise do gray opening
  251.   if (type == "binary") {
  252.     if (src.getMember (member: "bits") > 1)
  253.       src = src.convert (to: (1));
  254.  
  255.     dst = src.erode_ipc (sestring: sestr
  256.             ).dilate_ipc (sestring: sestr
  257.             );
  258.   } else {
  259.     dst = src.invert (
  260.             ).grayerode (file: sestr
  261.             ).graydilate (file: sestr
  262.             ).invert (
  263.             );
  264.   }
  265. }
  266.  
  267. /* @XIPClose
  268.   // DESCRIPTION
  269.   Performs binary/gray morphological dilation followed by erosion on a given
  270.   XIPIMAGE using the same structuring element. The closed image is returned as
  271.   the XIPIMAGE object "dst".
  272.   
  273.   // ARGUMENTS
  274.   XIPIMAGE src        Image to close
  275.   STREL    strel      Structuring element
  276.   
  277.   // OPTIONAL ARGUMENTS
  278.   STRING   type       By specifying type as "binary", processing will route
  279.                       through XEngine erode_ipc and dilate_ipc modules
  280.                       (adaptations of IPCore binary erosion and dilation
  281.                       routines, respectively, that supports 1 bit grayscale
  282.                       images only). Otherwise, it routes through XEngine
  283.                       grayerode and graydilate modules (supports 8 bit
  284.                       grayscale images).
  285.   
  286.   // RETURNS
  287.   XIPIMAGE dst        Closed image
  288. */
  289. private
  290. PROCEDURE XIPClose (XIPIMAGE src, STREL strel, STRING type)
  291.   RETURNS (XIPIMAGE dst)
  292. {
  293.   STRING sestr;
  294.  
  295.   // Need at least gray colorspace to process
  296.   if (src.getMember (member: "seps") > 1)
  297.     src = src.cspace (outspace: "gray");
  298.  
  299.   // Convert structuring element to a string
  300.   sestr = strel.toString ();
  301.  
  302.   // Do binary closing if type is "binary"; otherwise do gray closing
  303.   if (type == "binary") {
  304.     if (src.getMember (member: "bits") > 1)
  305.       src = src.convert (to: (1));
  306.  
  307.     dst = src.dilate_ipc (sestring: sestr
  308.             ).erode_ipc (sestring: sestr
  309.             );
  310.   } else {
  311.     dst = src.invert (
  312.             ).graydilate (file: sestr
  313.             ).grayerode (file: sestr
  314.             ).invert (
  315.             );
  316.   }
  317. }
  318.